home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / unixtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  3.4 KB  |  185 lines

  1. /*
  2.  Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
  3.  
  4. This file is part of GNU Common Lisp, herein referred to as GCL
  5.  
  6. GCL is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GCL is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public License 
  17. along with GCL; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. */
  21.  
  22. /*
  23.     unixtime.c
  24. */
  25.  
  26. #define IN_UNIXTIME
  27.  
  28. #include "include.h"
  29. #include <sys/types.h>
  30. #ifdef UNIX
  31. /* all we want from this is HZ the number of clock ticks per second
  32. which is usually 60 maybe 100 or something else. */
  33. #undef PAGESIZE
  34. #include <sys/param.h>
  35. #endif
  36. #ifndef HZ
  37. #define HZ 60
  38. #endif
  39.  
  40. #ifdef USE_ATT_TIME
  41. #undef BSD
  42. #define ATT
  43. #endif
  44.  
  45. #ifdef BSD
  46. #include <sys/timeb.h>
  47. #include <sys/times.h>
  48. #include <sys/time.h>
  49. static struct timeb beginning;
  50. #endif
  51.  
  52. #ifdef ATT
  53. #include <sys/times.h>
  54. long beginning;
  55. #endif
  56.  
  57. #ifdef E15
  58. #include <sys/times.h>
  59. long beginning;
  60. #endif
  61.  
  62. #ifdef DGUX
  63.  
  64.  
  65. #endif
  66.  
  67. runtime()
  68. {
  69.     struct tms buf;
  70.  
  71.     times(&buf);
  72.     return(buf.tms_utime);
  73. }
  74.  
  75. object
  76. unix_time_to_universal_time(i)
  77. int i;
  78. {
  79.     object x;
  80.     vs_mark;
  81.  
  82.     vs_push(make_fixnum(24*60*60));
  83.     vs_push(make_fixnum(70*365+17));
  84.     x = number_times(vs_top[-1], vs_top[-2]);
  85.     vs_push(x);
  86.     vs_push(make_fixnum(i));
  87.     x = number_plus(vs_top[-1], vs_top[-2]);
  88.     vs_reset;
  89.     return(x);
  90. }
  91.  
  92. Lget_universal_time()
  93. {
  94.     check_arg(0);
  95.     vs_push(unix_time_to_universal_time(time(0)));
  96. }
  97.  
  98. Lsleep()
  99. {
  100.     object z;
  101.     
  102.     check_arg(1);
  103.     check_type_or_rational_float(&vs_base[0]);
  104.     if (number_minusp(vs_base[0]) == TRUE)
  105.         FEerror("~S is not a non-negative number.", 1, vs_base[0]);
  106.     Lround();
  107.     z = vs_base[0];
  108.     if (type_of(z) == t_fixnum)
  109.         sleep(fix(z));
  110.     else
  111.         for(;;)
  112.             sleep(1000);
  113.     vs_top = vs_base;
  114.     vs_push(Cnil);
  115. }
  116.  
  117. Lget_internal_run_time()
  118. {
  119.     struct tms buf;
  120.  
  121.     check_arg(0);
  122.     times(&buf);
  123.     vs_push(make_fixnum(buf.tms_utime));
  124.     vs_push(make_fixnum(buf.tms_cutime));
  125.     
  126. }
  127.  
  128. Lget_internal_real_time()
  129. {
  130. #ifdef BSD
  131.     static struct timeval begin_tzp;
  132.     struct timeval tzp;
  133.     check_arg(0);
  134.     if (begin_tzp.tv_sec==0)
  135.       gettimeofday(&begin_tzp,0);
  136.     gettimeofday(&tzp,0);
  137. /* the value returned will be relative to the first time this is called,
  138.    plus the fraction of a second.  We must make it relative, so this
  139.    will only wrap if the process lasts longer than 818 days
  140.    */
  141.     vs_push(make_fixnum((tzp.tv_sec-begin_tzp.tv_sec)*HZ
  142.                 + ((tzp.tv_usec)*HZ)/1000000));
  143.  
  144. #endif
  145.  
  146. #ifdef ATT
  147.     check_arg(0);
  148.     vs_push(make_fixnum((time(0) - beginning)*HZ));
  149. #endif
  150.  
  151. #ifdef E15
  152.     check_arg(0);
  153.     vs_push(make_fixnum((time(0) - beginning)*HZ));
  154. #endif
  155.  
  156. #ifdef DGUX
  157.  
  158.  
  159. #endif
  160. }
  161.  
  162. init_unixtime()
  163. {
  164. #ifdef BSD
  165.     ftime(&beginning);
  166. #endif
  167. #ifdef ATT
  168.     beginning = time(0);
  169. #endif
  170. #ifdef E15
  171.     beginning = time(0);
  172. #endif
  173. #ifdef DGUX
  174.  
  175. #endif
  176.  
  177.     make_si_special("*DEFAULT-TIME-ZONE*", make_fixnum(TIME_ZONE));
  178.     make_constant("INTERNAL-TIME-UNITS-PER-SECOND", make_fixnum(HZ));
  179.  
  180.     make_function("GET-UNIVERSAL-TIME", Lget_universal_time);
  181.     make_function("SLEEP", Lsleep);
  182.     make_function("GET-INTERNAL-RUN-TIME", Lget_internal_run_time);
  183.     make_function("GET-INTERNAL-REAL-TIME", Lget_internal_real_time);
  184. }
  185.